package com.yicong.ants.view;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.widget.PopupWindow;

import java.util.HashMap;
import java.util.Map;

/**
 * PopupWindow 基础工具类
 */
public class CommonPop extends PopupWindow {

    private static boolean isShowBg;
    private static float bg_level;
    private PopupController controller;
    private Context context;

    private CommonPop(Context context) {
        this.context = context;
        controller = new PopupController(context, this);
    }

    @Override
    public int getWidth() {
        return controller.getmPopupView().getMeasuredWidth();
    }

    @Override
    public int getHeight() {
        return controller.getmPopupView().getMeasuredHeight();
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            setHeight(context.getResources().getDisplayMetrics().heightPixels - rect.bottom);
        }
        initWindowBg();
        super.showAsDropDown(anchor, xoff, yoff, gravity);
    }

    private void initWindowBg() {
        if (isShowBg) {
            controller.setBackGroundLevel(bg_level);
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        initWindowBg();
        super.showAtLocation(parent, gravity, x, y);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        controller.setBackGroundLevel(1.0f);
    }

    public interface ViewListener {
        void callBack();
    }

    public static class Builder {
        private final PopupController.PopupParams params;
        private Map<Integer, ViewListener> listenerMap;

        public Builder(Context context) {
            params = new PopupController.PopupParams(context);
        }

        /**
         * @param layoutResId 设置PopupWindow 布局ID
         * @return Builder
         */
        public Builder setView(int layoutResId) {
            params.mView = null;
            params.layoutResId = layoutResId;
            return this;
        }

        /**
         * @param view 设置PopupWindow布局
         * @return Builder
         */
        public Builder setView(View view) {
            params.mView = view;
            params.layoutResId = 0;
            return this;
        }

        /**
         * 为 View 设置点击事件
         *
         * @param viewId
         * @param listener
         * @return
         */
        public Builder setViewOnclickListener(int viewId, ViewListener listener) {
            if (null == listenerMap) {
                listenerMap = new HashMap<>();
            }
            listenerMap.put(viewId, listener);
            return this;
        }

        /**
         * 设置宽度和高度 如果不设置 默认是wrap_content
         *
         * @param width 宽
         * @return Builder
         */
        public Builder setWidthAndHeight(int width, int height) {
            params.mWidth = width;
            params.mHeight = height;
            return this;
        }

        /**
         * 设置背景灰色程度
         *
         * @param level 0.0f-1.0f
         * @return Builder
         */
        public Builder setBackGroundLevel(float level) {
            isShowBg = true;
            bg_level = level;
            return this;
        }

        /**
         * 是否可点击Outside消失
         *
         * @param touchable 是否可点击
         * @return Builder
         */
        public Builder setOutsideTouchable(boolean touchable) {
            params.isTouchable = touchable;
            return this;
        }

        /**
         * 设置动画
         *
         * @return Builder
         */
        public Builder setAnimationStyle(int animationStyle) {
            params.isShowAnim = true;
            params.animationStyle = animationStyle;
            return this;
        }

        public CommonPop create() {
            CommonPop popupWindow = new CommonPop(params.mContext);
            params.apply(popupWindow.controller, listenerMap);
            // measureWidthAndHeight(popupWindow.controller.mPopupView);
            return popupWindow;
        }

        /**
         * 测量View的宽高
         * @param view View

        private void measureWidthAndHeight(View view) {
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(widthMeasureSpec, heightMeasureSpec);
        }

         */

    }
}


